package team.lourd.network.system;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.context.WebServerPortFileWriter;
import org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import team.lourd.network.system.config.FileStorageProperties;

import javax.annotation.PostConstruct;
import java.util.TimeZone;

/**
 * Create by Lourd on 2019/1/17
 * 核心启动框架
 */
@EntityScan(basePackageClasses = {
        NetworkSystemApplication.class,
        Jsr310JpaConverters.class
})
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableConfigurationProperties({
        FileStorageProperties.class
})
// 开启异步操作
@EnableAsync
// 开启Quartz
@EnableScheduling
// 开启HAL Browser 功能
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@SpringBootApplication
public class NetworkSystemApplication {

    // 使用默认时区
    @PostConstruct
    void init() {
        TimeZone.setDefault(TimeZone.getDefault());
    }

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(NetworkSystemApplication.class);
        /** 使用脚本方式关闭应用程序，将应用程序上下文的PID写入到文件
         /* 将以下内容写成.bat或者是bash脚本
         /* kill $(cat ./shutdown.pid)
         **/
        springApplication.addListeners(new ApplicationPidFileWriter("./shutdown.pid"));
        springApplication.addListeners(new WebServerPortFileWriter("./port.txt"));
        SpringApplication.run(NetworkSystemApplication.class, args);
    }

}

